package com.dragon.www.smarthome.activity;

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;

import com.dragon.www.smarthome.R;
import com.dragon.www.smarthome.adapter.HarerLampAdapter;
import com.dragon.www.smarthome.entity.Device;
import com.dragon.www.smarthome.view.TitleView;

import org.dragonnova.lamp.ErrorCode;
import org.dragonnova.lamp.Lamp;
import org.dragonnova.lamp.LampController;
import org.dragonnova.lamp.LampListener;

import java.util.ArrayList;
import java.util.List;

/**
 * 灯
 */
public class HaierLampActivity extends FragmentActivity implements View.OnClickListener, AdapterView.OnItemClickListener {
    private ListView listView;
    private TitleView titleView;
    private HarerLampAdapter adapter;
    private List<Device> deviceList = new ArrayList<>();
    boolean isContinue = false;

    private LampController mLampController;
    private List<Lamp> mLamps = new ArrayList<>();

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mLampController = LampController.getInstance(this);
        mLampController.setLampListener(new LampListener() {
            @Override
            public void onRefresh(List<Lamp> lamps) {
                mLamps.clear();
                mLamps.addAll(lamps);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onError(ErrorCode error) {

            }
        });

        mLampController.start();
        mLampController.discovery();

        initView();
        initData();
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mLampController.stop();

    }

    public void initView() {
        setContentView(R.layout.activity_haier_lamp);
        listView = (ListView) findViewById(R.id.listView);
        titleView = (TitleView) findViewById(R.id.item_title);
        titleView.setTitle("灯光");
        titleView.setTextBackAction(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        adapter = new HarerLampAdapter(this, mLamps, mLampController);
        listView.setAdapter(adapter);
        listView.setOnItemClickListener(this);
    }

    public void initData(){

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.splitbutton:
                changLight(view);
                break;
        }
    }

    private void changLight(View view) {
        int position = listView.getPositionForView(view);
        Device device = (Device) listView.getItemAtPosition(position);
        Log.e("haier", device.getDeviceName());
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

    }



    //***********************************测试***********************************************************//

//    private void initData() {
//        // 测试 获取汉尔姆信息 用户登录请求
//        NetHelper.userLogin(this, "test", "123456", new NetUtils.NetCallBack<ServiceResult>() {
//            @Override
//            public void success(ServiceResult rspData) {
//                if (rspData.getCode().equals("0000")) {
//                    User user = new User();
//                    user.setUserName("test");
//                    Config.setUserInfo(user);
//                    getRoomFromNetwork();
//                } else {
//                    Toast.makeText(HaierLampActivity.this, "登录操作失败，原因" + rspData.getMessage(), Toast.LENGTH_SHORT).show();
//                }
//            }
//
//            @Override
//            public void failed(String msg) {
//                Toast.makeText(HaierLampActivity.this, msg, Toast.LENGTH_SHORT).show();
//            }
//        }, LoginInfo.class);
//        new Thread(new Runnable() {
//            @Override
//            public void run() {
//                while (true) {
//                    viewHandler.sendEmptyMessage(0);
//                    try {
//                        Thread.sleep(500);
//                    } catch (InterruptedException e) {
//                    }
//                }
//            }
//        }).start();
//    }
//
//    /**
//     * 从网络获取房间及设备
//     * 2016/06/01
//     */
//    public void getRoomFromNetwork() {
//        NetHelper.getRooms(this, new NetUtils.NetCallBack<ServiceResult>() {
//            @Override
//            public void success(ServiceResult rspData) {
//                if (rspData.getCode().equals("0000")) {
//                    //保存房间信息
//                    final RoomListInfo roomListInfo = (RoomListInfo) rspData;
//                    List<Room> mRooms = roomListInfo.getRooms();
//                    for (Room room : mRooms) {
//                        Log.e("Haier", room.getRoomName());
//                        if (room.getRoomName().contains("开放式办公")) {
//                            Config.setRoomId(room.getRoomid());
//                            Config.setRoomName(room.getRoomName());
//                            Config.setRoomDesc(room.getRoomDesciption());
//                            Config.setRoomBg(room.getImgPath());
//                            getRoomDeviceFromNetwork(room);
//                        }
//                    }
//                } else {
//                    Toast.makeText(HaierLampActivity.this, "获取房间信息失败，原因" + rspData.getMessage(), Toast.LENGTH_SHORT).show();
//                }
//            }
//
//            @Override
//            public void failed(String msg) {
//                Toast.makeText(HaierLampActivity.this, msg, Toast.LENGTH_SHORT).show();
//            }
//        }, RoomListInfo.class);
//    }
//
//
//    /**
//     * 从网络获取房间对应的设备
//     * 2016/06/01
//     */
//    public void getRoomDeviceFromNetwork(Room selectRoom) {
//        if (selectRoom != null) {
//            NetHelper.getDevices(HaierLampActivity.this, selectRoom.getRoomid(), new NetUtils.NetCallBack<ServiceResult>() {
//                @Override
//                public void success(ServiceResult rspData) {
//                    if (rspData.getCode().equals("0000")) {
//                        final DeviceListInfo deviceListInfo = (DeviceListInfo) rspData;
//                        deviceList.clear();
//                        deviceList.addAll(deviceListInfo.getDevices());
//                        adapter.setData(deviceList);
//                        adapter.notifyDataSetChanged();
//                        isContinue = true;
//                    } else {
//                        Toast.makeText(HaierLampActivity.this, "获取房间中的设备失败，原因" + rspData.getMessage(), Toast.LENGTH_SHORT).show();
//                    }
//                }
//
//                @Override
//                public void failed(String msg) {
//                    Toast.makeText(HaierLampActivity.this, msg, Toast.LENGTH_SHORT).show();
//                }
//            }, DeviceListInfo.class);
//        }
//    }
//
//    @Override
//    protected void onStop() {
//        super.onStop();
//        isContinue = false;
//    }
//
//    /**
//     * 每隔500毫秒刷新UI
//     * 2016/06/01
//     */
//    private final Handler viewHandler = new Handler() {
//        @Override
//        public void handleMessage(Message msg) {
//            switch (msg.what) {
//                case 0:
//                    NetHelper.getTypeDevices(HaierLampActivity.this, 11, "relay", new NetUtils.NetCallBack<DeviceInfo>() {
//
//                        @Override
//                        public void success(DeviceInfo rspData) {
//                            if (rspData.getError() == 0) {
//                                final InitializeRelayInfo deviceListInfo = (InitializeRelayInfo) rspData;
//                                InitializeRelayValueInfo[] result = deviceListInfo.getRelayInfo();
//                                adapter.setValue(result);
//                                Log.w("请求成功", "初始化成功");
//                            } else {
//                                String DD = rspData.getDescription();
//                                Log.w("错误描述", DD);
//                            }
//                        }
//
//                        @Override
//                        public void failed(String msg) {
//                            Toast.makeText(HaierLampActivity.this, msg, Toast.LENGTH_SHORT).show();
//                        }
//                    }, InitializeRelayInfo.class);
//                    break;
//            }
//            super.handleMessage(msg);
//        }
//    };


//********************************************测试结束*****************************************//
}
